/*
ID: EopXD
LANG: C++
TASK: UVA 11060
*/
#include <cstdio>
#include <cstring>
#include <algorithm>

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <queue>
#define SZ(x) (int)((x).size())
typedef long long LL;
using namespace std;
int n,m;
map<string,int> bev;
map<int,string> bck; int id;
int mp[102][102];
int in[102];
vector<string> out;
void set () {
    bev.clear();
    bck.clear();
    out.clear();
    memset(in,0,sizeof(in));
    memset(mp,0,sizeof(mp));
    id=0;
}
void topo ( int n ) {
    priority_queue < int, vector<int>, greater<int> > q;
    for ( int i=0; i<n; i++ ) if ( in[i]==0 ) q.push(i);
    while ( !q.empty() ) {
        int tmp=q.top(); q.pop();
        out.push_back(bck[tmp]);
        for ( int i=0; i<n; i++ ) {
            if ( mp[tmp][i] ) {
                mp[tmp][i]=0;
                if ( --in[i]==0 ) q.push(i);
            }
        }
    }
}
int main ()
{
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    int cas=1;
    while ( scanf("%d",&n)!=EOF ) {
        set();
        string s1,s2;
        for ( int i=0; i<n; i++ ) {
            cin>>s1;
            bev[s1]=id;
            bck[id++]=s1;
        }
        scanf("%d",&m);
        for ( int i=0; i<m; i++ ) {
            cin>>s1>>s2;
            if ( mp[bev[s1]][bev[s2]]==0 ) {
                mp[bev[s1]][bev[s2]]=1;
                in[bev[s2]]++;  
            }   
        }
        topo(n);
        printf("Case #%d: Dilbert should drink beverages in this order:",cas++);
        for ( int i=0; i<n; i++ ) cout<<" "<<out[i];
        printf(".\n\n");
    }
//  fclose(stdin);
//  fclose(stdout);
//  system("pause");
    return 0;
}
//---------------------------------------